<?php
class SystemOptionService extends Service {
	//所有设置的值
	var $opt	=	array();
	var $group	=	'';
	
    /**
     +----------------------------------------------------------
     * 架构函数
     +----------------------------------------------------------
     * @author melec制作
     * @access public
     +----------------------------------------------------------
     */
    public function __construct($data) {
		//1.判断是否安装、是否运行该服务，系统服务可以不做判断
		//2.服务初始化
		$this->init($data);
		$this->run();
    }

	//服务初始化
	public function init($data=''){
        if($this->group==''){
			$this->group     =   'thinksns';
		}
	}

	//运行服务，系统服务自动运行
	public function run(){
		//获取本组所有配置
		//todo：缓存
		$options	=	D('Option')->where("appname='$this->group'")->findAll();
		foreach((array) $options as $v){
			$this->opt[$v['name']]	=	$v['value'];	
		}
	}

	//设置数据组
	public function setGroup($group_name){
		$this->group	=	$group_name;
	}

	/**
     +----------------------------------------------------------
     * 获取配置记录
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $name 配置名称
     * @param string $group 配置分组
     +----------------------------------------------------------
     * @return string|array
     +----------------------------------------------------------
     */
	public function get($name,$group=false){
		if(!$group){
			return $this->opt[$name];
		}else{
			return $this->read($name,$group);
		}
	}

	/**
     +----------------------------------------------------------
     * 临时更改设置项，不保存到数据库
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $name 名称
     * @param mixed $value 值
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     */
	public function set($name,$value,$group=false){
		if(!$group){
			$group	=	$this->group;
		}
		$this->opt[$name]  =   $value;
	}

	//获取所有的记录 groupView 分组视图
	public function getAll($groupView=true){
		$options	=	D('Option')->findAll();
		if($groupView){
			foreach((array) $options as $v){
				$groupViewOptions[$v['group']][$v['name']]	=	$v['value'];	
			}
			return $groupViewOptions;
		}else{
			return $options;
		}
	}

    /**
     +----------------------------------------------------------
     * 从数据库读取设置项
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $name 名称
     * @param mixed $value 值
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     */
	public function read($name,$group=false){
		if(!$group){
			$group	=	$this->group;
		}
		$value	=	D('Option')->where("appname='$group' AND name='$name'")->findAll();
		return $value;
	}

    /**
     +----------------------------------------------------------
     * 保存设置项到数据库
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $name 名称
     * @param mixed $value 值
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     */
	public function save($name,$value,$group){
	
	}

    /**
     +----------------------------------------------------------
     * 从数据库中销毁设置项
     +----------------------------------------------------------
     * @access protected
     +----------------------------------------------------------
     * @param string $name 名称
     * @param mixed $value 值
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     */
	protected function destroy($name,$group){
	
	}

    /**
     +----------------------------------------------------------
     * 设置数据对象的值
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $name 名称
     * @param mixed $value 值
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     */
    public function __set($name,$value) {
        // 设置数据对象属性
        $this->set($name,$value);
    }

    /**
     +----------------------------------------------------------
     * 获取数据对象的值
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $name 名称
     +----------------------------------------------------------
     * @return mixed
     +----------------------------------------------------------
     */
    public function __get($name) {
        return isset($this->opt[$name])?$this->get($name):null;
    }

	/* 后台管理相关方法 */

	//启动服务，未编码
	public function _start(){
		return true;
	}
	
	//停止服务，未编码
	public function _stop(){
		return true;
	}

	//卸载服务，未编码
	public function _install(){
		return true;
	}

	//卸载服务，未编码
	public function _uninstall(){
		return true;
	}
}
?>